// 测试报名成功页面的数据流和显示逻辑
// Test the data flow and display logic of the registration success page

// 模拟从岗位申请页面传递的数据
const mockJobApplicationData = {
  company: '东莞高步电子厂',
  position: '不体检 普通工衣',
  salary: '22元/时',
  address: '广东省东莞市高埗镇三联村百旺科技园区',
  detailAddress: '广东省东莞市高埗镇三联村百旺科技园区A栋3楼',
  arrivalMethod: '自行到厂',
  hireRecruitUseId: 'recruit_001',
  hireRecruitUseEntryId: 'entry_001',
  createdAt: Date.now()
}

// 模拟页面参数编码
const mockEncodedData = encodeURIComponent(JSON.stringify(mockJobApplicationData))

// 测试数据解码
function testDataDecoding() {
  console.log('=== 测试数据解码 ===')
  console.log('原始数据:', mockJobApplicationData)
  console.log('编码后数据:', mockEncodedData)
  
  try {
    const decodedData = JSON.parse(decodeURIComponent(mockEncodedData))
    console.log('解码后数据:', decodedData)
    console.log('数据完整性检查:', {
      company: decodedData.company === mockJobApplicationData.company,
      position: decodedData.position === mockJobApplicationData.position,
      salary: decodedData.salary === mockJobApplicationData.salary,
      address: decodedData.address === mockJobApplicationData.address,
      detailAddress: decodedData.detailAddress === mockJobApplicationData.detailAddress,
      arrivalMethod: decodedData.arrivalMethod === mockJobApplicationData.arrivalMethod
    })
  } catch (error) {
    console.error('数据解码失败:', error)
  }
}

// 测试页面参数处理
function testPageParameterHandling() {
  console.log('\n=== 测试页面参数处理 ===')
  
  // 模拟页面参数
  const mockOptions = {
    data: mockEncodedData
  }
  
  if (mockOptions.data) {
    try {
      const decodedData = JSON.parse(decodeURIComponent(mockOptions.data))
      console.log('从页面参数获取的数据:', decodedData)
      
      // 模拟signup对象更新
      const signup = {
        company: decodedData.company || '',
        position: decodedData.position || '',
        salary: decodedData.salary || '',
        arrivalMethod: decodedData.arrivalMethod || '',
        address: decodedData.address || '',
        detailAddress: decodedData.detailAddress || ''
      }
      
      console.log('更新后的signup对象:', signup)
      return signup
    } catch (error) {
      console.error('处理页面参数失败:', error)
      return null
    }
  } else {
    console.log('没有页面参数，使用本地存储数据')
    return null
  }
}

// 测试UI显示逻辑
function testUIDisplayLogic(signup: any) {
  console.log('\n=== 测试UI显示逻辑 ===')
  
  if (!signup) {
    console.log('signup数据为空，显示默认值')
    return
  }
  
  // 测试岗位信息卡片显示
  console.log('岗位信息卡片显示:')
  console.log('- 报名企业:', signup.company || '企业名称')
  console.log('- 岗位:', signup.position || '—')
  console.log('- 工价:', signup.salary || '—')
  console.log('- 面试地址:', signup.detailAddress || signup.address || '—')
  console.log('- 到厂方式:', signup.arrivalMethod || '—')
  
  // 测试地图区域显示
  console.log('\n地图区域显示:')
  console.log('- 地址:', signup.address || '地址信息')
  console.log('- 企业名称:', signup.company || '企业名称')
  
  // 测试面试详情显示
  console.log('\n面试详情显示:')
  console.log('- 面试企业:', signup.company || '企业名称')
  console.log('- 面试地址:', signup.detailAddress || signup.address || '地址信息')
  console.log('- 到厂方式:', signup.arrivalMethod || '自行到厂')
}

// 测试数据回退逻辑
function testFallbackLogic() {
  console.log('\n=== 测试数据回退逻辑 ===')
  
  // 模拟本地存储数据
  const mockLocalStorageData = {
    company: '东莞卡士莫',
    position: '普工',
    salary: '20元/时',
    arrivalMethod: '专车接送',
    createdAt: Date.now() - 86400000 // 1天前
  }
  
  console.log('本地存储数据:', mockLocalStorageData)
  
  // 模拟没有页面参数的情况
  const mockOptions = {}
  
  if (mockOptions.data) {
    console.log('有页面参数，使用页面参数数据')
  } else {
    console.log('没有页面参数，使用本地存储数据')
    const signup = mockLocalStorageData
    console.log('回退后的signup对象:', signup)
    return signup
  }
}

// 测试边界情况
function testEdgeCases() {
  console.log('\n=== 测试边界情况 ===')
  
  // 测试空数据
  const emptyData = {
    company: '',
    position: '',
    salary: '',
    address: '',
    detailAddress: '',
    arrivalMethod: ''
  }
  
  console.log('空数据处理:')
  console.log('- 企业名称:', emptyData.company || '企业名称')
  console.log('- 岗位:', emptyData.position || '—')
  console.log('- 工价:', emptyData.salary || '—')
  console.log('- 地址:', emptyData.address || '地址信息')
  console.log('- 详细地址:', emptyData.detailAddress || emptyData.address || '地址信息')
  console.log('- 到厂方式:', emptyData.arrivalMethod || '自行到厂')
  
  // 测试部分数据
  const partialData = {
    company: '东莞高步电子厂',
    position: '',
    salary: '22元/时',
    address: '',
    detailAddress: '广东省东莞市高埗镇三联村百旺科技园区A栋3楼',
    arrivalMethod: ''
  }
  
  console.log('\n部分数据处理:')
  console.log('- 企业名称:', partialData.company || '企业名称')
  console.log('- 岗位:', partialData.position || '—')
  console.log('- 工价:', partialData.salary || '—')
  console.log('- 地址:', partialData.address || '地址信息')
  console.log('- 详细地址:', partialData.detailAddress || partialData.address || '地址信息')
  console.log('- 到厂方式:', partialData.arrivalMethod || '自行到厂')
}

// 运行所有测试
function runAllTests() {
  console.log('开始测试报名成功页面数据流...\n')
  
  testDataDecoding()
  const signup = testPageParameterHandling()
  testUIDisplayLogic(signup)
  testFallbackLogic()
  testEdgeCases()
  
  console.log('\n所有测试完成！')
}

// 执行测试
runAllTests()
